<?php
/**
 * Created by PhpStorm.
 * User: xinfeng
 * Date: 2017/8/30
 * Time: 20:28
 */

header("Access-Control-Allow-Origin: *");
$file = $_FILES['chooseImg'];file_put_contents('111.ttt', $file);
$url = '/public/userChatImg/';
$upto = dirname(UP360_ROOT) .$url;
$newName = md5(time().rand(1, 100000)).substr($file['name'], strripos($file['name'], '.'));
$url = $url.$newName;
if ($file['error'] == 0) {
	str_replace('/', '\\', $upto);
	if ($file['size'] > 10485760) {
		echo json_encode(array('status'=>-1, 'message'=>'文件过大'));
	}
	if(!is_dir($upto)){
		makeDir($upto, 755);
	}
	move_uploaded_file($file['tmp_name'], $upto.$newName);
	echo json_encode(array('status'=>0, 'imgUrl'=>$url), true);
}

function makeDir($dir)
{
	$dir = preg_replace('/[\/\\\]/', DIRECTORY_SEPARATOR, $dir);  //kevin add
	$arrDir	= explode(DIRECTORY_SEPARATOR, $dir );	//edit by yangjs 2010.07.02

	$subDir	= '';

	foreach( $arrDir as $t )
	{
		$subDir	.= $t.DIRECTORY_SEPARATOR;

		if( !is_dir($subDir) )
		{
			if( ! mkdir( $subDir,0777 ) )
			{
				echo $subDir,'<br/>';
				return false;
			}
			@chmod($subDir,0777);
		}
	}
	return true;
}